<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
<meta content="yes" name="apple-mobile-web-app-capable">
<meta content="black" name="apple-mobile-web-app-status-bar-style">
<meta content="telephone=no" name="format-detection">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/bootstrap/easyui.css">
<link rel="stylesheet" type="text/css" href="../static/easyui/themes/icon.css">
<link rel="stylesheet" href="../static/css/style-app.css" />
<script src="../static/jquery/jquery-1.12.4.js"></script>
<script src="../static/easyui/jquery.easyui.min.js"></script>
<script src="../static/easyui/locale/easyui-lang-zh_CN.js"></script>
<title>主计划管理</title>
</head>
<body>
	<div class="top-part">
		<div class="header">
			<div class="return">
				<a href="javascript:history.go(-1);"></a>
			</div>
			<div class="header-title">主计划管理</div>
		</div>
	</div>
	<div class="new-form">
		<div>
			<form id="ff" method="post">
				<table cellpadding="5" style="margin: auto;">
					<tr>
						<td style="width: 100px;">项目：</td>
						<td id="width1">
							<input style="width: 100%;" id="projectitem" name="projectitem" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    url: '../projectitemlist.do?status=2',
							    onSelect: function(rec){
								    if(rec.projectitemchilds.length>0){
										document.getElementById('tr1').style.display = '';
										document.getElementById('tr2').style.display = '';
										var url = '../projectitemchildlist?status=2&&projectitemid=' + rec.id;
										$('#projectitemchild').combobox('reload', url);
								    }else{
										document.getElementById('tr1').style.display = 'none';
										document.getElementById('tr2').style.display = 'none';
								   		$.ajaxSettings.async = false;
								    	$.get('../defaultPlanMainName?id='+rec.id,
											function(result){
											$('#name').textbox('setValue','阶段'+result);
											findRemainPercent(rec.id);
									    });
							    	}
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td id="tr1" style="display: none;">子项目：</td>
						<td id="tr2" style="display: none;">
							<input style="width: 100px;" id="projectitemchild" name="projectitemchild" class="easyui-combobox"
								editable="false"
								data-options="
							    valueField: 'id',
							    textField: 'name',
							    onSelect: function(rec){
							   		$.ajaxSettings.async = false;
							    	$.get('../defaultPlanMainNameChild?id='+rec.id,
										function(result){
										$('#name').textbox('setValue','阶段'+result);
										findRemainPercentChild(rec.id);
									});
							    },panelHeight: 'auto'">
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">标题：</td>
						<td colspan="3">
							<input class="easyui-textbox" id="name" name="name" style="width: 100%;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">内容：</td>
						<td colspan="3">
							<input class="easyui-textbox" data-options="multiline:true" id="content" name="content"
								style="width: 100%; height: 100px;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">开始日期：</td>
						<td>
							<input class="easyui-datebox" data-options="editable:false" id="startdate"
								name="startdate" style="width: 100%;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">结束日期：</td>
						<td>
							<input class="easyui-datebox" data-options="editable:false,validType:'end'" id="enddate"
								name="enddate" style="width: 100%;" />
						</td>
					</tr>
					<tr>
						<td style="width: 100px;">形象进度：</td>
						<td colspan="3">
							<input class="easyui-textbox" data-options="validType:['number','percent']" id="percent" name="percent"
								style="width: 100%;" />
						</td>
					</tr>
					<tr>
						<td></td>
						<td class="text-center">
							<label>剩余形象进度：</label>
							<label id="remainpercent"></label>
							<label>%</label>
						</td>
					</tr>
				</table>
			</form>
			<div style="text-align: center;" class="search-input" id="div1">
				<a href="#" class="d-btn btn-blue" onclick="savePro()">保存</a>
			</div>
		</div>
	</div>
</body>
<script th:inline="javascript">
	var currentPro = [[${planMain}]];

	$("#projectitemchild").combobox({ width:$("#width1").width()}); 
	$(document).ready(function() {
		if(currentPro != null){
			$("#projectitem").combobox("setValue", currentPro.item.id);
			if (currentPro.projectitemchild != null){
				$("#projectitemchild").combobox("setValue", currentPro.projectitemchild.id);
				getRemainChildPercent();
			}else{
				getRemainPercent();
			}
			$("#content").textbox("setValue", currentPro.content);
			$("#percent").textbox("setValue", currentPro.percent);
			$("#name").textbox("setValue", currentPro.name);
			$("#startdate").datebox("setValue", formatDate(currentPro.startdate));
			$("#enddate").datebox("setValue", formatDate(currentPro.enddate));
			if(currentPro.status != 6){
				document.getElementById('div1').style.display = 'none';
			}
		}else{
			currentPro = {};
		}
	});
	function savePro() {
		var projectitemid = $("#projectitem").combobox('getValue');
		currentPro.projectitemid = projectitemid;
		var projectchildid = $("#projectitemchild").combobox('getValue');
		if (projectchildid != null){
			currentPro.projectitemchildid = projectchildid;
		}
		var name = $("#name").textbox('getValue');
		currentPro.name = name;
		var content = $("#content").textbox('getValue');
		currentPro.content = content;
		var startdate = $("#startdate").datebox('getValue');
		currentPro.startdate = startdate;
		var enddate = $("#enddate").datebox('getValue');
		currentPro.enddate = enddate;
		var percent = $("#percent").textbox('getValue');
		currentPro.percent = percent;
		$.ajax({
			type : 'POST',
			url : "../planMainSave.do",
			data : JSON.stringify(currentPro),
			contentType : "application/json",
			success : function(data) {
				if (data.success){
					$.messager.alert('保存主计划', '保存主计划成功', 'success');
				}else{
					$.messager.alert('保存主计划', '保存主计划失败', 'error');
				}
				window.setInterval("window.location.href=document.referrer", 2000);
			},
			dataType : "json"
		});
	}
	function getRemainPercent() {
		var data = {};
		data.planid = currentPro.id;
		data.projectitemid = currentPro.projectitemid;
		$.ajax({
			type : 'GET',
			url : "../planmain/remainpercent",
			data : data,
			contentType : "application/json",
			success : function(data) {
				if (data){
					$("#remainpercent").text(data);
				}
			},
			dataType : "text"
		});
	}
	function getRemainChildPercent() {
		var data = {};
		data.planid = currentPro.id;
		data.projectitemchildid = currentPro.projectitemchildid;
		$.ajax({
			type : 'GET',
			url : "../planmain/remainpercentchild",
			data : data,
			contentType : "application/json",
			success : function(data) {
				if (data){
					$("#remainpercent").text(data);
				}
			},
			dataType : "text"
		});
	}
	function findRemainPercent(id) {
		var data = {};
		data.planid = -1;
		data.projectitemid = id;
		$.ajax({
			type : 'GET',
			url : "../planmain/remainpercent",
			data : data,
			contentType : "application/json",
			success : function(data) {
				if (data){
					$("#remainpercent").text(data);
				}
			},
			dataType : "text"
		});
	}
	function findRemainPercentChild(id) {
		var data = {};
		data.planid = -1;
		data.projectitemchildid = id;
		$.ajax({
			type : 'GET',
			url : "../planmain/remainpercentchild",
			data : data,
			contentType : "application/json",
			success : function(data) {
				if (data){
					$("#remainpercent").text(data);
				}
			},
			dataType : "text"
		});
	}
	function formatDate(time) {
		var date = new Date(time);
		var year = date.getFullYear(),month = date.getMonth() + 1, //月份是从0开始的
		day = date.getDate(),hour = date.getHours(),min = date.getMinutes(),sec = date.getSeconds();
		var newTime = year + '-' + month + '-' + day;
		return newTime;
	}
	$.extend($.fn.textbox.defaults.rules, {
		number : {//value值为文本框中的值
			validator : function(value) {
				var reg = /^\d+(\.\d+)?$/;
				return reg.test(value);
			},
			message : "请输入数字"
		},
		chinese : {
			validator : function(value,param) {
				var reg = /^[\u4e00-\u9fa5]+$/i;
				return reg.test(value);
			},
			message : "请输入中文"
		},
		checkLength : {
			validator : function(value,param) {
				return param[0] >= get_length(value);
			},
			message : '请输入最大{0}位字符'
		},
		specialCharacter : {
			validator : function(value,param) {
				var reg = new RegExp("[`~!@#$^&*()=|{}':;'\\[\\]<>~！@#￥……&*（）——|{}【】‘；：”“'、？]");
				return !reg.test(value);
			},
			message : '不允许输入特殊字符'
		},
		end : {
			validator : function(value,param) {
				var startDate = $("#startdate").val();
				var startTmp = new Date(startDate.replace(/-/g, "/"));
				var endTmp = new Date(value.replace(/-/g, "/"));
				return startTmp <= endTmp;
			},
			message : '结束时间不能早于开始时间！'
		},
		percent : {
			validator : function(value,param) {
				var percent = $("#remainpercent").text();
				return parseFloat(value) <= parseFloat(percent);
			},
			message : '不能超过剩余形象进度'
		}
	});
</script>

</html>